<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  /*body{*/

  /*}*/
  .weui-cell_select {
    padding: 10px 15px;
  }
  .weui-cells__title {
    text-align: left;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
</style>
<template>
  <div class="sendcar">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      {{title}}
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="../../assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
        <form action="/api/sellform/add" method="post" id="formid">
          <input type="hidden" name="type" value="1" />
          <div class="cellgroup">
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">领料单编号</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text" readonly :value="pickinglist.code" name="">
            </div>
          </div>
            <div class="weui-cell">
              <div class="weui-cell__hd"><label class="weui-label">货品所属</label></div>
              <div class="weui-cell__bd">
                <input class="weui-input" readonly  type="text"  name="" :value="pickinglist.orgname">
              </div>
            </div>
            <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">所在班组</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" readonly  type="text"  name="" :value="pickinglist.departmentname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">入库库房</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" readonly name="" :value="pickinglist.strname">
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">创建时间</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" readonly type="text" name="" :value="pickinglist.createtime">
            </div>
          </div>
        </div>
          <ApplySrItem v-for="(item, index) in oitems" :key="index" :itemNo="item" :isLast="index+1 == oitems.length" :products="products" :type="type" @doAddItem="addItem(index + 1)" @doDelItem="delItem"></ApplySrItem>

          <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary halfbtn subbtn"  href="javascript:" @click="send">提交</a>
          <a class="weui-btn weui-btn_primary halfbtn"  href="javascript:" @click="$router.back(-1)" id="sub">返回</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  import ApplySrItem from "./ApplySrItem";
  export default {
    name: 'ApplyStoreRm',
    data () {
      return {
        openid: localStorage.openid,
        guid: '',
        type: 1,
        pickinglist: {},
        items: [],
        products: [],
        specifications: [],
        product1:"",
        product2:"",
        product3:"",
        product4:"",
        product5:"",
        oitems: [0],
        nodeid: '',
        title: ''
      }
    },
    components: {ApplySrItem},
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
      })
    },
    mounted () {
      let that = this;
      this.guid = this.$route.query.guid;
      //this.type = this.$route.query.type;
      this.nodeid = this.$route.query.nodeid;
      this.title = this.$route.query.title;
      //获取回显数据
      this.getApplyInfo(this.$route.query.guid);

      //添加明细item
      $('form').on('click', '#addItem', function () {
        $('.oitem:hidden').eq(0).find('input').attr("disabled",false);
        $('.oitem:hidden').eq(0).find('textarea').attr("disabled",false);
        $('.oitem:hidden').eq(0).find('select').attr("disabled",false);
        $('.oitem:hidden').eq(0).show();
        updateActBtns();
      })
      //删除明细
      $('form').on('click', '#delItem', function () {
        var elm = $(this).parents('.oitem');
        $.confirm("确定要删除该货品明细吗？", function () {
          elm.find('input').val('');
          elm.find('textarea').val('');
          elm.hide();
          $('.oitem:hidden').eq(0).find('input').attr("disabled",true);
          $('.oitem:hidden').eq(0).find('textarea').attr("disabled",true);
          $('.oitem:hidden').eq(0).find('select').attr("disabled",true);
          updateActBtns();
        })
      })
      //调整添加、删除按钮位置
      function updateActBtns() {
        $('#acts').remove();
        var actHtml = '<p id="acts" style="float: right;color: #1aad19;"><span id="addItem" style="padding-right: 5px;">添加</span><span>|</span><span id="delItem" style="padding-left: 5px;">删除</span></p>';

        if ($('.oitem:visible').length == 5) {
          actHtml = '<p id="acts" style="float: right;color: #1aad19;"><span id="delItem" style="padding-left: 5px;">删除</span></p>';
        } else if ($('.oitem:visible').length == 1) {
          var actHtml = '<p id="acts" style="float: right;color: #1aad19;"><span id="addItem" style="padding-right: 5px;">添加</span></p>';
        }
        $('.oitem:visible:last').find('.weui-cells__title').append(actHtml);
      }
    },
    computed: {
      typetitle: function () {
        let title = '';
        switch (this.type) {
          case 1:
            title = '成品入库申请';
            break;
          case 2:
            title = '次品入库申请';
            break;
        }
        return title;
      },
    },
    methods: {
      addItem: function(index) {
        this.oitems.push(index);
      },
      delItem: function() {
        this.oitems.pop();
      },
      getApplyInfo: function(guid) {
        this.axios({
          method: 'get',
          url: '/produce/Whinapply!getApplyInfo.action',
          params: {pickinglistid: guid}
        }).then((response) => {
          if (response.data.msg == 'success') {
            let data = response.data;
            this.pickinglist = data.pickinglist;
            this.items = data.items;
            this.products = data.products;
            //this.specifications = data.specifications;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      send: function() {
        if (!this.checkEmy('product')) {
          this.toastText('产品不能为空！');
          return false;
        } else if (!this.checkEmy('spec')) {
          this.toastText('规格不能为空！');
          return false;
        }

        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");

        let postData = this.serialize2Obj($("#formid").serializeArray());
        postData.nodeid = this.nodeid;
        this.axios({
          method: 'post',
          url: '/produce/Whinapply!saveApplyInfo.action?openid=' + this.openid + '&pickinglistid=' + this.guid,
          //params: $("#formid").serialize()
          params: postData
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");

          if(response.data.msg == "success"){
            this.toastText('操作成功!');
            this.$router.push({name: 'PickingList'});
          }else if (response.data.msg == 'outlogin'){
            this.toastError('请重新登录!');
          } else if (response.data.msg == 'canotopt') {
            this.toastError('不允许的操作！');
          } else if (response.data.msg == 'error'){
            this.toastError('系统错误!');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          this.toastError('系统错误');
        });

      }

    }
  }


</script>
